package top.cluski.feieprint.api.maiyatian;

import cn.hutool.core.text.StrFormatter;
import com.alibaba.fastjson2.JSON;
import com.dtflys.forest.Forest;
import com.dtflys.forest.utils.TypeReference;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import top.cluski.feieprint.api.maiyatian.domain.dto.WebhookCreateOrderDTO;
import top.cluski.feieprint.api.maiyatian.domain.vo.WebhookCreateOrderVO;
import top.cluski.feieprint.api.maiyatian.domain.vo.WebhookOrderSummaryVO;
import top.cluski.feieprint.api.maiyatian.domain.vo.WebhookOrderVO;
import top.cluski.feieprint.config.MaiyatianConfig;

import java.util.List;

@Component
public class MaiyatianWebhookApi {
    @Resource
    MaiyatianConfig maiyatianConfig;

    public List<WebhookOrderVO> getTodayOrders() {
        return Forest.get(StrFormatter.format("{}/maiyatian/todayOrders", maiyatianConfig.getWebhookBaseUrl()))
                .execute(new TypeReference<>() {
                });
    }

    public List<WebhookOrderVO> getYesterdayOrders() {
        return Forest.get(StrFormatter.format("{}/maiyatian/yesterdayOrders", maiyatianConfig.getWebhookBaseUrl()))
                .execute(new TypeReference<>() {
                });
    }

    public WebhookCreateOrderVO createOrder(WebhookCreateOrderDTO webhookCreateOrderDTO) {
        String respStr = Forest.post(StrFormatter.format("{}/maiyatian/order", maiyatianConfig.getWebhookBaseUrl()))
                .contentTypeJson()
                .addBody(webhookCreateOrderDTO)
                .executeAsString();
        return JSON.parseObject(respStr, WebhookCreateOrderVO.class);
    }

    public WebhookOrderSummaryVO getYesterdayOrderSummary() {
        return Forest.get(StrFormatter.format("{}/maiyatian/yesterdayOrderSummary", maiyatianConfig.getWebhookBaseUrl()))
                .execute(WebhookOrderSummaryVO.class);
    }
}
